/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.client;

import com.daikit.commons.shared.bean.DkBaseUser;
import com.daikit.daikit4gxt.client.DkBaseMainController;
import com.daikit.daikit4gxt.client.action.DkBaseAction;
import com.daikit.daikit4gxt.client.log.DkLogger;
import com.daikit.daikit4gxt.client.rpc.BaseRpcMiscsAsync;
import com.daikit.daikit4gxt.client.rpc.BaseRpcUserAsync;
import com.daikit.daikit4gxt.client.screen.DkBaseScreen;
import com.daikit.daikit4gxt.client.screen.DkWelcomeScreen;
import com.daikit.daikit4gxt.client.ui.DkBaseGui;
import com.daikit.daikit4gxtsample.client.action.SpecificAfterConnectAction;
import com.daikit.daikit4gxtsample.client.rpc.RpcMiscs;
import com.daikit.daikit4gxtsample.client.rpc.RpcMiscsAsync;
import com.daikit.daikit4gxtsample.client.rpc.RpcProduct;
import com.daikit.daikit4gxtsample.client.rpc.RpcProductAsync;
import com.daikit.daikit4gxtsample.client.rpc.RpcUser;
import com.daikit.daikit4gxtsample.client.rpc.RpcUserAsync;
import com.daikit.daikit4gxtsample.client.screen.testscreen.TestScreen;
import com.daikit.daikit4gxtsample.shared.bean.User;
import com.google.gwt.core.client.GWT;


/**
 * Main application controller for MVC
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class MainController extends DkBaseMainController
{

	private final DkLogger log = DkLogger.getLog(MainController.class);

	/**
	 * Base constructor
	 * 
	 * @param gui
	 */
	public MainController(final DkBaseGui gui)
	{
		super(gui);
	}

	@Override
	public Class<? extends DkBaseScreen> getDefaultScreen()
	{
		final User loggedUser = Main.model().getUserLogged();
		if (loggedUser == null)
		{
			return DkWelcomeScreen.class;
		}
		return TestScreen.class;
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// OVERRIDDEN METHODS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	@Override
	public DkBaseAction<?> getSpecificAfterConnectAction(final DkBaseUser previousLoggedUser)
	{
		return SpecificAfterConnectAction.get(previousLoggedUser);
	}

	@Override
	protected <S extends DkBaseScreen> DkBaseScreen getSpecificScreenInstance(final Class<S> clazz)
	{
		DkBaseScreen ret = null;
		if (DkWelcomeScreen.class.getName().equals(clazz.getName()))
		{
			ret = new DkWelcomeScreen();
		}
		else if (TestScreen.class.getName().equals(clazz.getName()))
		{
			ret = new TestScreen();
		}
		else
		{
			log.error("Given screen class is not handled in client.controller.getSpecificScreenInstance(...) - screen class="
					+ clazz);
		}
		return ret;
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// RPCS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	private RpcProductAsync rpcProduct;

	/**
	 * Get RPC product
	 * 
	 * @return the {@link RpcProductAsync}
	 */
	public RpcProductAsync rpcProduct()
	{
		if (rpcProduct == null)
		{
			rpcProduct = GWT.create(RpcProduct.class);
		}
		return rpcProduct;
	}

	/**
	 * Overrides base RPC user to provide custom functions. Call {@link #rpcUser()} instead.
	 */
	@Override
	public BaseRpcUserAsync baseRpcUser()
	{
		if (rpcUser == null)
		{
			rpcUser = GWT.create(RpcUser.class);
		}
		return rpcUser;

	}

	/**
	 * Overrides base RPC miscs to provide custom functions. Call {@link #rpcMiscs()} instead.
	 */
	@Override
	public BaseRpcMiscsAsync baseRpcMiscs()
	{
		if (rpcMiscs == null)
		{
			rpcMiscs = GWT.create(RpcMiscs.class);
		}
		return rpcMiscs;
	}

	/**
	 * Get the RPC User
	 * 
	 * @return the {@link RpcUserAsync}
	 */
	public RpcUserAsync rpcUser()
	{
		return (RpcUserAsync) baseRpcUser();
	}

	/**
	 * Get the RPC Miscs
	 * 
	 * @return the {@link RpcMiscsAsync}
	 */
	public RpcMiscsAsync rpcMiscs()
	{
		return (RpcMiscsAsync) baseRpcMiscs();
	}

}
